package org.xinjiang.data.migration.xinjiang.convert;

import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.xinjiang.data.migration.common.constant.CreateConstant;
import org.xinjiang.data.migration.common.mapstruct.CommonConvertConfig;
import org.xinjiang.data.migration.xinjiang.dto.res.XjSubDcpgResultDTO;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;

@Mapper(config = CommonConvertConfig.class)
public abstract class XjDcpgConvert {
    @Autowired
    private XjJzdxConvert xjJzdxConvert;

    public void postAddAttribute(XjSubDcpgResultDTO record) {
        if (Objects.isNull(record)) {
            return;
        }

        //委托编号为空处理
        setOrderNumbers(record);

        //设置原判刑罚为汉字的情况
        record.setYpxf(xjJzdxConvert.changePenalty(record.getYpxf()));

        //设置被告人出生日期
        record.setBgrcsrq(setBirthday(record.getBgrcsrq()));

        //设置状态
        record.setZt(statusChange(record.getZt()));
    }

    private void setOrderNumbers(XjSubDcpgResultDTO record) {
        if (StringUtils.isEmpty(record.getWtbh())) {
            record.setWtbh(String.valueOf(System.currentTimeMillis()));
        }
    }

    private Date setBirthday(Date birthday) {
        if (Objects.isNull(birthday)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate parsedDate = LocalDate.parse(CreateConstant.DEFAULT_BIRTHDAY, formatter);
            return Date.from(parsedDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        }
        return birthday;
    }

    /**
     * 1-县级待指派，2-所待接收，3-所待提交，4-县待提交，0-结束，-1所拒绝，-2县拒绝
     *
     * @param source 状态
     * @return String
     */
    private String statusChange(String source) {
        String result = "0";
        if (StringUtils.isEmpty(source)) {
            return result;
        }
        switch (source) {
            case "1":
                result = "1";
                break;
            case "2":
                result = "0";
                break;
        }

        return result;
    }
}
